package org.ccf.certified.controller.shiro;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.ccf.certified.bean.ResponseBean;
import org.ccf.certified.bean.pagination.PageRequest;
import org.ccf.certified.business.log.annotation.LogAnnotation;
import org.ccf.certified.business.shiro.manager.PermissionManager;
import org.ccf.certified.business.shiro.model.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author LiuFang
 * @date 2022/1/12
 */
@RestController
@RequestMapping("/web/api/permission")
public class PermissionController {

    @Autowired
    private PermissionManager permissionManager;

    @LogAnnotation(module = "新增或修改资源信息")
    @PostMapping
    public ResponseBean save(@RequestBody Permission permission){
        boolean save = permissionManager.saveOrUpdate(permission);
        return save ? ResponseBean.getResultsSuccess() : ResponseBean.getResultsFail();
    }

    @LogAnnotation(module = "删除资源信息")
    @DeleteMapping
    public ResponseBean edit(@RequestParam Long id){
        boolean save = permissionManager.removeById(id);
        return save ? ResponseBean.getResultsSuccess() : ResponseBean.getResultsFail();
    }

    @LogAnnotation(module = "分页查询角色信息")
    @GetMapping("/page")
    public ResponseBean page(PageRequest pageRequest) {
        IPage page = permissionManager.page(pageRequest.convertToPage());
        return ResponseBean.getResultsSuccess(page);
    }

    @LogAnnotation(module = "根据主键查询角色")
    @GetMapping("/{id}")
    public ResponseBean getById(@PathVariable Long id){
        Permission permission = permissionManager.getById(id);
        return ResponseBean.getResultsSuccess(permission);
    }
}
